/*
 * UserInterface.c
 *      Author: DePhille
 */


/**
 * This function is called when the config frame is requested.
 */
static GtkWidget* PC_UI_Load(PurplePlugin* plugin) {
	GtkNotebook*	notebook;		// Clean-up: No
	GtkWidget*		notebookWidget;	// Clean-up: No

	// Create a notebook:
	notebookWidget = gtk_notebook_new();
	notebook = (GtkNotebook*) notebookWidget;
	if (!notebook) {
		purple_debug_error("PidginCote", "PC_UI_Load: Unable to make notebook.\r\n");
		return NULL;
	}
	g_signal_connect(notebook, "destroy", G_CALLBACK(PC_UI_Unload), NULL);

	// Show widget:
	gtk_widget_show(notebookWidget);

	// Fill notebook:
	PC_UI_MakePage_Account(notebook);

	// Return notebook:
	return notebookWidget;
}


/**
 * This function is called when the config frame is closed.
 */
static void PC_UI_Unload(GtkObject* object, gpointer data) {
	// Do nothing.
}


/**
 * Adds a page to the notebook.
 */
static GtkWidget* PC_UI_AddPage(GtkNotebook* notebook, const gchar* label) {
	GtkWidget*	vBox;		// Clean-up: No
	GtkWidget*	labelObj;	// Clean-up: No

	// Check input:
	if (!notebook) {
		purple_debug_warning("PidginCote", "PC_UI_AddPage: Missing 'notebook' parameter.\r\n");
		return NULL;
	} else if (!label) {
		purple_debug_warning("PidginCote", "PC_UI_AddPage: Missing 'label' parameter.\r\n");
		return NULL;
	}

	// Make a VBox:
	vBox = gtk_vbox_new(FALSE, 6);
	if (!vBox) {
		purple_debug_error("PidginCote", "PC_UI_AddPage: Unable to make 'vBox'.\r\n");
		return NULL;
	}
	gtk_widget_show(vBox);

	// Make a label:
	labelObj = gtk_label_new_with_mnemonic(label);
	if (!labelObj) {
		purple_debug_error("PidginCote", "PC_UI_AddPage: Unable to make 'label'.\r\n");
		return NULL;
	}
	gtk_widget_show(labelObj);

	// Add page to notebook:
	gtk_notebook_append_page(notebook, vBox, labelObj);

	// Return the vBox:
	return vBox;
}


/**
 * Makes the Account tab.
 *
 * @param	notebook	The notebook under which to make the Account tab.
 */
static void PC_UI_MakePage_Account(GtkNotebook* notebook) {
	GtkWidget*	vBox;				// Clean-up: No
	GtkWidget*	frmAuthentication;	// Clean-up: No

	// Check input:
	if (!notebook) {
		purple_debug_warning("PidginCote", "PC_UI_MakePage_Account: Missing 'notebook' parameter.\r\n");
		return;
	}

	// Make a page:
	vBox = PC_UI_AddPage(notebook, "_Account");
	if (!vBox) {
		purple_debug_error("PidginCote", "PC_UI_MakePage_Account: Unable to add page.\r\n");
		return;
	}

	// Make authentication frame:
	frmAuthentication = PC_UI_MakeFrame_Authentication();
	if (!frmAuthentication) {
		purple_debug_warning("PidginCote", "PC_UI_MakePage_Account: Unable to make Auth frame.\r\n");
	} else {
		gtk_box_pack_end(GTK_BOX(vBox), frmAuthentication, TRUE, FALSE, 10);
	}

}

/**
 * Makes a frame which contains the basic Authentication information.
 *
 * @return	A frame with Auth info.
 */
static GtkWidget* PC_UI_MakeFrame_Authentication(void) {
	GtkWidget*		frmAuthentication;	// Clean-up: No
	GtkWidget*		username;			// Clean-up: No
	GtkWidget*		password;			// Clean-up: No
	GtkWidget*		loginButton;		// Clean-up: No
	GtkWidget*		vBox;				// Clean-up: No
	GtkWidget*		lblUsername;		// Clean-up: No
	GtkWidget*		lblPassword;		// Clean-up: No
	GtkSizeGroup*	sizeGroup;			// Clean-up: No

	// Make frame:
	frmAuthentication = gtk_frame_new("Authentication");
	gtk_widget_show(frmAuthentication);

	// Make a vBox:
	vBox = gtk_vbox_new(TRUE, 5);
	gtk_widget_show(vBox);
	gtk_container_add(GTK_CONTAINER(frmAuthentication), vBox);

	// Make sizeGroup:
	sizeGroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);



	// Make username part:
	lblUsername = gtk_label_new_with_mnemonic("Username");
	gtk_widget_show(lblUsername);
	username = gtk_entry_new();
	gtk_entry_set_text(GTK_ENTRY(username), purple_prefs_get_string("/plugins/gtk/gtk-dephille-pidgincote/username"));
	gtk_widget_show(username);
	pc_ui_username = username;
	gtk_box_pack_start(GTK_BOX(vBox), PC_UI_MakeRow(lblUsername, sizeGroup, username, NULL), FALSE, FALSE, 0);

	// Make password part:
	lblPassword = gtk_label_new_with_mnemonic("Password");
	gtk_widget_show(lblPassword);
	password = gtk_entry_new();
	gtk_widget_show(password);
	gtk_entry_set_visibility(GTK_ENTRY(password), FALSE);
	gtk_entry_set_text(GTK_ENTRY(password), purple_prefs_get_string("/plugins/gtk/gtk-dephille-pidgincote/password"));
	pc_ui_password = password;
	gtk_box_pack_start(GTK_BOX(vBox), PC_UI_MakeRow(lblPassword, sizeGroup, password, NULL), FALSE, FALSE, 0);

	// Make Save button part:
	loginButton = gtk_button_new_with_mnemonic("_Save");
	gtk_widget_show(loginButton);
	gtk_box_pack_start(GTK_BOX(vBox), loginButton, FALSE, FALSE, 0);
	g_signal_connect(loginButton, "clicked", G_CALLBACK(PC_UI_CB_SaveAuth), NULL);

	// Return frame:
	return frmAuthentication;
}


/**
 * Makes a simple row containing two widgets.
 *
 * @param	widget1		The first widget.
 * @param	sizeGroup1	GtkSizeGroup, if any, for the first widget.
 * @param	widget2		The second widget.
 * @param	sizeGroup2	GtkSizeGorup, if any, for the second widget.
 * @return	A HBox containing the two widgets.
 */
static GtkWidget* PC_UI_MakeRow(GtkWidget* widget1, GtkSizeGroup* sizeGroup1, GtkWidget* widget2, GtkSizeGroup* sizeGroup2) {
	GtkWidget*	hBox;	// Clean-up: No

	// Check input:
	if (!widget1) {
		purple_debug_warning("PidginCote", "PC_UI_MakeRow: Missing 'widget1' parameter.\r\n");
		return NULL;
	} else if (!widget2) {
		purple_debug_warning("PidginCote", "PC_UI_MakeRow: Missing 'widget2' parameter.\r\n");
		return NULL;
	}

	// Make HBox:
	hBox = gtk_hbox_new(FALSE, 10);
	gtk_widget_show(hBox);

	// Add widgets:
	gtk_box_pack_start(GTK_BOX(hBox), widget1, FALSE, FALSE, 10);
	gtk_box_pack_start(GTK_BOX(hBox), widget2, FALSE, FALSE, 10);

	// Assign sizegroups:
	if (sizeGroup1) {
		gtk_size_group_add_widget(sizeGroup1, widget1);
	}
	if (sizeGroup2) {
		gtk_size_group_add_widget(sizeGroup2, widget2);
	}

	// Return:
	return hBox;
}


static void	PC_UI_CB_SaveAuth(GtkButton* button, gpointer user_data) {
	// Store information:
	purple_prefs_set_string("/plugins/gtk/gtk-dephille-pidgincote/username", gtk_entry_get_text(GTK_ENTRY(pc_ui_username)));
	purple_prefs_set_string("/plugins/gtk/gtk-dephille-pidgincote/password", gtk_entry_get_text(GTK_ENTRY(pc_ui_password)));

	// Reset token:
	if (pc_net_token) {
		g_free(pc_net_token);
		pc_net_token = NULL;
	}

	// Log in:
	PC_Event_Login(NULL);
}
